package com.mlz.mlzmall.ui.activity.frag.main;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.ProductIntroduceBean;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.dialog.ReuestDialog;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Pm on 2016/5/18.
 */
public class BrandFragment extends Fragment {
    public ProductIntroduceBean.ManufacturerInfoDto datas;
    private ListView listView;
    private Context context;
    String a;//关键字
    String b;
    String c;
    String d;
    String e;
    String f;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        View rootView = inflater.inflate(R.layout.fragment_brand, container, false);//关联布局文件
        listView = (ListView) rootView.findViewById(R.id.lv_manufacturer_intro);
        intiData();
        return  rootView;
    }
    public void intiData()
    {
        ReuestDialog.showProgressDialog(getActivity());
    //    JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzmall.com/api/manufacturer/info?manufacturerId=" + SharedUtils.get(getActivity(),"manufacturerId",0), null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.MANUFACTURER.INFO+"?manufacturerId=" + SharedUtils.get(getActivity(),"manufacturerId",0), null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ReuestDialog.dissmissProgressDialog();
                LogUtils.d("获得品牌介绍信息：" + jsonObject.toString());
                Gson gson = new Gson();
                ProductIntroduceBean productIntroduceBean = gson.fromJson(jsonObject.toString(),ProductIntroduceBean.class);
                datas = productIntroduceBean.getData();
                if(datas!=null) {
                    listView.setAdapter(new MyAdapter1());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("intiData", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + (String) SharedUtils.get(getActivity(), ConfigInfo.SESSIONKEY, ""));
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("intiData");
        // 将请求添加到队列中
        ((MyApplication) getActivity().getApplication()).getRequestQueue().add(jsonRequest);
    }
    private class MyAdapter1 extends BaseAdapter {
        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(R.layout.topic_good_introduce_item, null);
            ViewHolder vh = new ViewHolder();
            vh.iv_opiv_picture1 = (ImageView) convertView.findViewById(R.id.iv_opiv_picture1);
            vh.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            vh.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
            vh.iv_nationalflag = (ImageView) convertView.findViewById(R.id.iv_nationalflag);
            vh.iv_say_title_picture =(ImageView) convertView.findViewById(R.id.iv_say_title_picture);
            vh.tv_title_picture_name = (TextView) convertView.findViewById(R.id.tv_title_picture_name);
            vh.tv_title_picture_title = (TextView) convertView.findViewById(R.id.tv_title_picture_title);
            vh.tv_picture_title = (TextView) convertView.findViewById(R.id.tv_picture_title);
            vh.btn_one= (Button) convertView.findViewById(R.id.btn_one);
            vh.btn_two= (Button) convertView.findViewById(R.id.btn_two);
            vh.btn_three= (Button) convertView.findViewById(R.id.btn_three);
            vh.btn_four= (Button) convertView.findViewById(R.id.btn_four);
            vh.btn_five= (Button) convertView.findViewById(R.id.btn_five);
            vh.btn_six = (Button)convertView.findViewById(R.id.btn_six);

            vh.rl_one = (RelativeLayout)convertView.findViewById(R.id.rl_one);
            vh.rl_two = (RelativeLayout)convertView.findViewById(R.id.rl_two);
            vh.rl_three = (RelativeLayout)convertView.findViewById(R.id.rl_three);
            vh.rl_four = (RelativeLayout)convertView.findViewById(R.id.rl_four);
            vh.rl_five = (RelativeLayout)convertView.findViewById(R.id.rl_five);
            vh.rl_six = (RelativeLayout)convertView.findViewById(R.id.rl_six);
            vh.iv_nationalflag = (ImageView)convertView.findViewById(R.id.iv_nationalflag);
            vh.webview_goods_introduce = (WebView)convertView.findViewById(R.id.wv_webview_goods_introduce);

            if (null != convertView) {
                if (datas!=null) {
                    String  url = datas.detailHref;

                    //此方法可以在webview中打开链接而不会跳转到外部浏览器
                    vh.webview_goods_introduce.getSettings().setJavaScriptEnabled(true);//webview的适配
                    vh.webview_goods_introduce.loadUrl(url);
                    ImageLoader.getInstance().displayImage(datas.image, vh.iv_opiv_picture1);

                  //  StringTools.nametopic=datas.getName();
                    LogUtils.d("StringTools.nametopic="+datas.getName());
                    vh.tv_goods_name.setText(datas.name);
                    vh.tv_text.setText(datas.content);
                    ImageLoader.getInstance().displayImage(datas.areaCode, vh.iv_nationalflag);
                    ImageLoader.getInstance().displayImage(datas.recommend.avatar, vh.iv_say_title_picture);
                    vh.tv_title_picture_name.setText(datas.recommend.recommendBy);
                    vh.tv_title_picture_title.setText(datas.recommend.recommendByRole);
                    vh.tv_picture_title.setText(datas.recommend.content);

                    String str = datas.getKeywords();
                    String[] strs = str.split(",");//切割

                    if (strs.length==1){//关键字的切割并显示
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                    }else if (strs.length==2){
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                        b = strs[1];
                        vh.rl_two.setVisibility(View.VISIBLE);
                        vh.btn_two.setText(b);//在TextView中显示数组内容。
                    }else if (strs.length==3) {
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                        b = strs[1];
                        vh.rl_two.setVisibility(View.VISIBLE);
                        vh.btn_two.setText(b);//在TextView中显示数组内容。
                        c = strs[2];
                        vh.rl_three.setVisibility(View.VISIBLE);
                        vh.btn_three.setText(c);//在TextView中显示数组内容。
                    }else if (strs.length==4) {
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                        b = strs[1];
                        vh.rl_two.setVisibility(View.VISIBLE);
                        vh.btn_two.setText(b);//在TextView中显示数组内容。
                        c = strs[2];
                        vh.rl_three.setVisibility(View.VISIBLE);
                        vh.btn_three.setText(c);//在TextView中显示数组内容。
                        d = strs[3];
                        vh.rl_four.setVisibility(View.VISIBLE);
                        vh.btn_four.setText(d);//在TextView中显示数组内容。
                    } else if (strs.length==5) {
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                        b = strs[1];
                        vh.rl_two.setVisibility(View.VISIBLE);
                        vh.btn_two.setText(b);//在TextView中显示数组内容。
                        c = strs[2];
                        vh.rl_three.setVisibility(View.VISIBLE);
                        vh.btn_three.setText(c);//在TextView中显示数组内容。
                        d = strs[3];
                        vh.rl_four.setVisibility(View.VISIBLE);
                        vh.btn_four.setText(d);//在TextView中显示数组内容。
                        e = strs[4];
                        vh.rl_five.setVisibility(View.VISIBLE);
                        vh.btn_five.setText(e);

                    }else if (strs.length==6) {
                        a = strs[0];
                        vh.rl_one.setVisibility(View.VISIBLE);
                        vh.btn_one.setText(a);//在TextView中显示数组内容。
                        b = strs[1];
                        vh.rl_two.setVisibility(View.VISIBLE);
                        vh.btn_two.setText(b);//在TextView中显示数组内容。
                        c = strs[2];
                        vh.rl_three.setVisibility(View.VISIBLE);
                        vh.btn_three.setText(c);//在TextView中显示数组内容。
                        d = strs[3];
                        vh.rl_four.setVisibility(View.VISIBLE);
                        vh.btn_four.setText(d);//在TextView中显示数组内容。
                        e = strs[4];
                        vh.rl_five.setVisibility(View.VISIBLE);
                        vh.btn_five.setText(e);
                        f = strs[5];
                        vh.rl_six.setVisibility(View.VISIBLE);
                        vh.btn_six .setText(e);
                    }

                    ImageLoader.getInstance().displayImage("http://m.mlzMall.com/Public/images/flags/" + datas.areaCode + ".png", vh.iv_nationalflag);
                }
            }
            return convertView;
        }

        class ViewHolder {
            ImageView iv_opiv_picture1;
            TextView tv_goods_name;
            ImageView iv_nationalflag;
            TextView tv_text ;
            ImageView iv_say_title_picture;
            TextView tv_title_picture_name ;
            TextView tv_title_picture_title ;
            TextView tv_picture_title;
            Button tv_import_text_say;
            WebView webview_goods_introduce;
            Button btn_one;
            Button btn_two;
            Button btn_three;
            Button btn_four;
            Button btn_five;
            Button btn_six;
            RelativeLayout rl_one;
            RelativeLayout rl_two;
            RelativeLayout rl_three;
            RelativeLayout rl_four;
            RelativeLayout rl_five;
            RelativeLayout rl_six;

        }
    }
}
